package com.example.mycarcalling.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.stp.StpUtil;
import com.example.mycarcalling.bean.Navigation;
import com.example.mycarcalling.bean.Order;
import com.example.mycarcalling.service.ApplicationService;
import com.example.mycarcalling.service.NavigationService;
import com.example.mycarcalling.service.OrderService;
import com.example.mycarcalling.service.UserService;
import com.example.mycarcalling.utils.BaseResponse;
import com.example.mycarcalling.utils.ResponseResult;
import com.example.mycarcalling.vo.Address;
import com.example.mycarcalling.vo.OrderWithNav;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
//@BaseResponse
@RequestMapping(value = "/navigation")
public class NavigationController {

    @Resource
    private NavigationService navigationService;

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    @Resource
    private ApplicationService applicationService;


    /**
     * 乘客发送导航信息，并生成订单
     * @param navigation
     * @param num
     * @param request
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/sendingOrder/{num}")
    public ResponseResult sendingOrder(@RequestBody Navigation navigation, @PathVariable("num") int num, HttpServletRequest request){
        Long navigationId = null;
        Long orderId = null;
        try {
            Long userId = StpUtil.getLoginIdAsLong();
            System.out.println(userId);
            navigation.setPublishingUser(userId);
            navigationId = navigationService.sendingNavigation(navigation);
            if(null == navigationId){
                throw new Exception("导航数据异常");
            }
            System.out.println(navigationId);


            Order order = new Order();
            order.setNavigationInfo(navigationId);
            order.setPassengerNumber(num);
            order.setTravelPassenger(userId);
            orderId = orderService.sendingOrder(order);
            if(null == orderId){
                throw new Exception("订单数据异常");
            }
            return new ResponseResult(200,"success",orderId.toString());
        }catch (NotLoginException e){
            return new ResponseResult(503,"failed",e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,"failed",e.getMessage());
        }

    }

    //获取推送的导航信息
    @RequestMapping(method = RequestMethod.POST,value ="getNavigationInfo")
    public ResponseResult getNavigationInfo(@RequestBody Address address,HttpServletRequest request){
        List<Navigation> navigationList = null;
        Integer passengerNumberPermit = null;
        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                passengerNumberPermit = applicationService.getDriverApplication(StpUtil.getLoginIdAsLong())
                        .getPassengerNumberPermit();
                if(null!=passengerNumberPermit) {
                    navigationList = navigationService.getLimitThreeNavigation(address,passengerNumberPermit);
                    if (null != navigationList) {
                        return new ResponseResult(200, "success", navigationList);
                    }
                }
                return new ResponseResult(201, "暂时无导航信息", null);
            }
            throw new Exception("没有司机权限");
        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }

    /**
     * 获取用户正在导航的信息
     * @param request 用户传递token，服务器获取用户id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "/getWaitingNavigation")
    public ResponseResult getWaitingNavigation(HttpServletRequest request){
        Navigation navigation;
        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())){
                throw new Exception("没有乘客权限");
            }
            navigation = navigationService.getNotFinishNavigation(StpUtil.getLoginIdAsLong());
            if(null != navigation){
                return new ResponseResult(200,"success",navigation);
            }
            return new ResponseResult(201,"暂无正在进行的导航信息",null);

        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }

    /**
     * 获取司机所有的历史订单信息
     * @param request
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "/getDriverAllOrderNav")
    public ResponseResult getDriverAllOrderNav(HttpServletRequest request){
        List<OrderWithNav> orderWithNavs = null;

        try {
            if (userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                orderWithNavs = navigationService.getDriverAllOrderNav(StpUtil.getLoginIdAsLong());
                if (null == orderWithNavs) {
                    System.out.println("返回201");
                    return new ResponseResult(201, "success", orderWithNavs);
                }
                System.out.println("返回200"+orderWithNavs);
                return new ResponseResult(200, "success", orderWithNavs);
            }
            throw new Exception("没有司机权限");
        }catch(NotLoginException e){
                e.printStackTrace();
                return new ResponseResult(503, e.getMessage(), null);
        }catch(Exception e){
                e.printStackTrace();
            System.out.println("返回500");
            return new ResponseResult(500, e.getMessage(), null);
        }


    }

    /**
     * 获取乘客所有的历史订单信息
     * @param request
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "/getPassengerAllOrderNav")
    public ResponseResult getPassengerAllOrderNav(HttpServletRequest request){
        List<OrderWithNav> orderWithNavs = null;

        try {
            if (userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                throw new Exception("没有乘客权限");
            }
            orderWithNavs = navigationService.getPassengerAllOrderNav(StpUtil.getLoginIdAsLong());
            if (null == orderWithNavs) {
                System.out.println("返回201");
                return new ResponseResult(201, "success", orderWithNavs);
            }
            System.out.println("返回200"+orderWithNavs);
            return new ResponseResult(200, "success", orderWithNavs);
        }catch(NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503, e.getMessage(), null);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("返回500");
            return new ResponseResult(500, e.getMessage(), null);
        }


    }

    /**
     * 获取上次进行的订单的导航信息
     * @param lastNvId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "getLastNavigation/{lastNvId}")
    public ResponseResult getLastNavigation(@PathVariable("lastNvId") Long lastNvId){
        Navigation navigation = null;
        try {
            StpUtil.getLoginIdAsLong();
            navigation = navigationService.getLastNavigation(lastNvId);
            if(null==navigation){
                throw new Exception("未找到数据");
            }
            return new ResponseResult(200,"success",navigation);
        }catch(NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503, e.getMessage(), null);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("返回500");
            return new ResponseResult(500, e.getMessage(), null);
        }
    }




}
